package cn.tedu.isharefront.base.interceptor;

import cn.tedu.isharefront.base.constant.JwtConstants;
import cn.tedu.isharefront.base.util.JWTUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 功能：Jwt拦截器
 *
 * @author yhl
 * @time 2025/3/1 23:06
 */
public class JwtInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String header = request.getHeader(JwtConstants.AUTHORIZATION);
        if("OPTIONS".equals(request.getMethod())){
            return true;
        }

        if(!StringUtils.isBlank(header)){
            String token = header.substring(JwtConstants.AUTHORIZATION_BEARER.length(),header.length());
            JWTUtil.verify(token);
            return true;
        }
        //response.setStatus(403);
        return false;
    }
}
